package cn.elead.chaos.remote.web.system.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.elead.chaos.core.enums.BusinessType;
import cn.elead.chaos.excel.ExcelUtil;
import cn.elead.chaos.framework.monitor.operlog.Log;
import cn.elead.chaos.mybatils.web.controller.BaseController;
import cn.elead.chaos.mybatils.web.page.TableDataInfo;
import cn.elead.chaos.remote.web.system.service.IRemoteUserService;
import cn.hutool.core.util.StrUtil;
import cn.elead.chaos.remote.web.system.domain.SysUser;

/**
 * 用户信息
 * 
 * @author luopeng
 */
@RestController
@RequestMapping("/remote/system/user")
public class RemoteUserController extends BaseController {

	@Autowired
	private IRemoteUserService userService;

	/**
	 * 获取用户列表
	 */
	@GetMapping("/page")
	public TableDataInfo<SysUser> page(SysUser user) {
		return userService.page(user);
	}

	/**
	 * 获取用户列表
	 */
	@GetMapping("/list")
	public List<SysUser> list(SysUser user) {
		return userService.list(user);
	}

	@GetMapping("/export")
	@RequiresPermissions("system:user:export")
	@Log(title = "用户管理", businessType = BusinessType.EXPORT)
	public String export(SysUser user) {
		List<SysUser> list = userService.list(user);
		ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
		return util.exportExcel(list, "用户数据");
	}

	/**
	 * 根据用户编号获取详细信息
	 */
	@GetMapping(value = { "/", "/{userId}" })
	@RequiresPermissions("system:user:list")
	public Map<String, Object> getInfo(@PathVariable(value = "userId", required = false) String userId) {
		return userService.getInfo(userId);
	}

	/**
	 * 新增用户
	 */
	@PostMapping
	@RequiresPermissions("system:user:add")
	@Log(title = "用户管理", businessType = BusinessType.INSERT)
	public int add(@Validated @RequestBody SysUser user) {
		return userService.add(user);
	}

	/**
	 * 修改用户
	 */
	@PutMapping
	@RequiresPermissions("system:user:edit")
	@Log(title = "用户管理", businessType = BusinessType.UPDATE)
	public int edit(@Validated @RequestBody SysUser user) {
		return userService.edit(user);
	}

	/**
	 * 删除用户
	 */
	@DeleteMapping("/{userIds}")
	@RequiresPermissions("system:user:remove")
	@Log(title = "用户管理", businessType = BusinessType.DELETE)
	public int remove(@PathVariable String[] userIds) {
		String ids = StringUtils.join(userIds, StrUtil.COMMA);
		return userService.remove(ids);
	}

	/**
	 * 状态修改
	 */
	@PutMapping("/changeStatus")
	@RequiresPermissions("system:user:edit")
	@Log(title = "用户管理", businessType = BusinessType.UPDATE)
	public int changeStatus(@RequestBody SysUser user) {
		return userService.changeStatus(user);
	}
}